home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / html / cgi-bin / mailform.cgi < prev    next >
Text File  |  1997-06-17  |  11KB  |  367 lines

  1. #!/usr/bin/perl
  2. # $Id: mailform.general,v 1.18 1996/11/08 02:57:25 kwong Exp $
  3. #
  4. # mailform.general - cgi script for mailing form output
  5. #     The following hidden fields are supported:
  6. #    encrypt        - The outgoing mail will be encrypted using the
  7. #              destination user's public key
  8. #        <INPUT TYPE="hidden" NAME="encrypt">
  9. #    footer        - specifies additional footer icons
  10. #        <INPUT TYPE="hidden" NAME="footer" VALUE="section:url:icon">
  11. #            section - the section name (alt text or text if no icon)
  12. #            url     - the URL to link to
  13. #            icon    - image URL to display (optional)
  14. #    nofieldnames    - eliminates printing of the field names
  15. #        <INPUT TYPE="hidden" NAME="nofieldnames">
  16. #    nospecial    - keep special fields in the form (ie e-mail, name)
  17. #        <INPUT TYPE="hidden" NAME="nospecial">
  18. #    required    - The fields specified are required for form 
  19. #              submission.  For this field, "name" is the name
  20. #              of the field, and "text" is the text to display
  21. #              as a field description.  For multiple required
  22. #              fields, use multiple hidden lines.
  23. #        <INPUT TYPE="hidden" NAME="required" VALUE="name:text">
  24. #    response    - The value of this field will be used as the
  25. #              document to send for the response to submitting
  26. #              the form.  The filename must be a full path
  27. #              relative to the DocumentRoot.
  28. #        <INPUT TYPE="hidden" NAME="response" VALUE="/filename">
  29. #    separator    - specifies the field separator (defaults to \n)
  30. #        <INPUT TYPE="hidden" NAME="separator" VALUE="\t">
  31. #    sort         - causes the output to be sorted by field name
  32. #        <INPUT TYPE="hidden" NAME="sort">
  33. #
  34. # $Log: mailform.general,v $
  35. # Revision 1.18  1996/11/08  02:57:25  kwong
  36. # At strip out code for \0 for non sort
  37. #
  38. # Revision 1.17  1996/09/19  17:55:12  kwong
  39. # .
  40. #
  41. # Revision 1.16  1996/09/16  18:56:14  kwong
  42. # .
  43. #
  44. # Revision 1.15  1996/06/12  20:41:27  kwong
  45. # .
  46. #
  47. # Revision 1.14  1995/11/09  20:48:45  drich
  48. # fixed a nospecial bug
  49. #
  50. # Revision 1.13  1995/11/08  18:19:59  drich
  51. # added field seperator and nospecial fields
  52. #
  53. # Revision 1.12  1995/10/24  23:54:25  drich
  54. # fixed docs, and added nofieldnames option
  55. #
  56. # Revision 1.11  1995/10/07  02:41:44  drich
  57. # fixed e-mail and subject fields
  58. #
  59. # Revision 1.10  1995/09/18  22:21:22  drich
  60. # fixed wording on error form
  61. #
  62. # Revision 1.9  1995/09/16  04:44:38  drich
  63. # changed footers tag to footer
  64. #
  65. # Revision 1.8  1995/09/16  04:06:53  drich
  66. # more footer fixes
  67. #
  68. # Revision 1.7  1995/09/16  03:47:39  drich
  69. # fixed footer bug
  70. #
  71. # Revision 1.6  1995/09/16  03:45:05  drich
  72. # added footer documentation
  73. #
  74. # Revision 1.5  1995/09/16  03:35:59  drich
  75. # Fixed bug for multiple required fields
  76. #
  77. # Revision 1.4  1995/09/16  00:09:35  kwong
  78. # .
  79. #
  80. # Revision 1.3  1995/09/15  22:10:18  drich
  81. # added lots of options
  82. #
  83. # Revision 1.2  1995/06/16  17:57:29  drich
  84. # Added support for CC to webmaster if from the feedback form
  85. #
  86. # Revision 1.1  1995/02/12  00:06:22  drich
  87. # Initial revision
  88. #
  89. #
  90.  
  91. # Get potential include path
  92. ($dirname,$basename) = ($0 =~ (/^(.*)\/([^\/]*)$/));
  93. push(@INC,$dirname)     if ($dirname);
  94.  
  95. require('cgi-lib-g.pl') || die "can\'t require cgi-lib.pl: $!";
  96. require('surf-lib-g.pl') || die "can\'t require surf-lib.pl: $!";
  97.  
  98. $request_method = $ENV{'REQUEST_METHOD'};
  99. $query_string = $ENV{'QUERY_STRING'};
  100. $path_info = $ENV{'PATH_INFO'};
  101. $path_info =~ s/^\///; $path_info =~ s/\// /;
  102. $content_length = $ENV{'CONTENT_LENGTH'};
  103.  
  104. sub error {
  105.     local(@error) = @_;
  106.  
  107.     &header("Mailform error!!");
  108.     
  109.     print "<B>\n";
  110.     print "There has been an error processing your mailform submission.\n";
  111.     print "</B><P><PRE>\n";
  112.     foreach $_ (@error) {
  113.     print $_;
  114.     }
  115.     print "</PRE>\n";
  116.  
  117.     &footer;
  118.     exit;
  119. }
  120.  
  121. # Save any error output
  122. close(STDERR);
  123. open(STDERR,">/usr/tmp/mailform$$.log") || die "can\'t open logfile:/usr/tmp/mailform$$.log: $!";
  124.  
  125. &ReadParse();
  126. # Get "special" entries
  127. foreach $key (keys %in) {
  128.     $email = $key    if (($key =~ /e-*mail/i) && ($in{$key} =~ /\@/));
  129.     $name = $key    if ($key =~ /name/i);
  130.     $subject = $key    if ($key =~ /subject/i);
  131.     $comment = $key    if ($key =~ /comments*/i);
  132. }
  133.  
  134. # Check for hidden fields to set options
  135. $sort = $in{'sort'} ? true : 0;        # Should the output be sorted?
  136. $sep = $in{'separator'} ? $in{'separator'} : "\n";
  137. $nospecial = $in{'nospecial'};        # 
  138. @required = split(/\\0/,$in{'required'});   # Required field names (name:text)
  139. $encrypt = $in{'encrypt'};
  140. $response = $in{'response'};        # Custom response?    
  141. @footers = split(/\\0/,$in{'footer'});    # Footer information
  142.  
  143. # Convert @footers to @sections for &footer
  144. foreach $footer (@footers) {
  145.     ($section,$url,$icon) = split(':',$footer);
  146.     push(@sections,$section);
  147.     $name{$section} = $section;
  148.     $root{$section} = $url;
  149.     $icon_url{$section} = $icon    if ($icon);
  150. }
  151.  
  152. foreach $info (@required) {
  153.     ($field,$fname) = split(':',$info);
  154.     $missing = !$in{$field}    unless($missing);
  155. }
  156.  
  157. if ($missing) {
  158.     &header("Missing information....");
  159.     
  160.     print <<"EOF";
  161. I'm sorry, but you haven't entered all of the required information on the
  162. form.  The form may not be submitted until the following information is provided:
  163. <P>
  164. EOF
  165.  
  166.     print "<UL>\n";
  167.     foreach $info (@required) {
  168.     ($field,$fname) = split(':',$info);
  169.     $fname = $field    unless($fname);
  170.     $fname =~ s/_/ /g;
  171.     print "  <LI>$fname\n"    unless($in{$field});
  172.     }
  173.     print "</UL>\n";
  174.     print " <P>\n";
  175. #    if (! $in{'DocName'}) {
  176. #    $in{'DocName'} = $ENV{'HTTP_REFERER'};
  177. #    }
  178.     ($server,$path) = ($in{'DocName'} =~ /http:\/\/([^\/:]+):*\d*\/(.*)$/);
  179.     if ($server && $server_name =~ /^$server/ && -f "$DocumentRoot/$path") {
  180.     print "<HR><BR>\n";
  181.     open(DOC,"$DocumentRoot/$path");
  182.     while(<DOC>) {
  183.         $_ = $save . $_;    # Saved line from last time
  184.  
  185.         unless (/</) {
  186.         print $_;
  187.         undef($save);
  188.         next;
  189.         }
  190.         if (/<\/FORM>/i) {
  191.         print "<INPUT TYPE=\"hidden\" NAME=\"DocName\" VALUE=\"$in{'DocName'}\">\n";
  192.         }
  193.         ($name) = /NAME\s*=\s*\"([^\"]+)"/i;
  194.         ($type) = /TYPE\s*=\s*\"([^\"]+)"/i;
  195.         if (!$name || $type =~ /hidden/i) {    # Print hidden fields as-is
  196.             print $_;
  197.         undef($save);
  198.         next;
  199.         }
  200.         if (/TEXTAREA/i) {
  201.         print $_;
  202.         print "$in{$name}\n";
  203.         undef($save);
  204.         next;
  205.         }
  206.         if ($in{$name}) {
  207.         if (/VALUE=/i) {
  208.             s/(VALUE\s*=\s*\")[^\"]*\"/$1$in{$name}\"/i;
  209.         } else {
  210.             s/(NAME\s*=\s*\"[^\"]*\")/$1 VALUE=\"$in{$name}\"/i;
  211.         }
  212.         }
  213.         print $_;
  214.         undef($save);
  215.     }
  216.     close(DOC);
  217.     } else {
  218.     &footer;
  219.     }
  220.     unlink("/usr/tmp/mailform$$.head.out");
  221.     unlink("/usr/tmp/mailform$$.body.out");
  222.     unlink("/usr/tmp/mailform$$.log");
  223.     exit 0;
  224. }
  225.  
  226. open(TMPMAILHEAD,">/usr/tmp/mailform$$.head.out") || 
  227.     die "can\'t open /usr/tmp/mailform$$.head.out: $!";
  228. if ($in{$email}) {
  229. #    open(MAIL,"|/usr/lib/sendmail -t -f $in{$email}");
  230.     print TMPMAILHEAD "To: $path_info\n";
  231.     if (defined $in{$name}) {
  232.         print TMPMAILHEAD "From: $in{$email} ($in{$name})\n";
  233.     } else {
  234.         print TMPMAILHEAD "From: $in{$email}\n";
  235.     }
  236. } else {
  237. #    open(MAIL,"|/usr/lib/sendmail -t");
  238.     print TMPMAILHEAD "To: $path_info\n";
  239.     print TMPMAILHEAD "From: Nobody\n";
  240. }
  241. print TMPMAILHEAD "Reply-To: $in{$email}\n"    if ($in{$email});
  242. print TMPMAILHEAD "Cc: webmaster\@www.sgi.com\n" if (($path_info !~ /^webmaster/) &&
  243.                           ($ENV{'HTTP_REFERER'} =~ /form_feedback/));
  244. if (defined $in{$subject}) {
  245.     print TMPMAILHEAD "Subject: $in{$subject}\n";
  246. } else {
  247.     print TMPMAILHEAD "Subject: Mail from WWW\n";
  248. }
  249. print TMPMAILHEAD "Precedence: bulk\n";
  250. print TMPMAILHEAD "\n";
  251. close(TMPMAILHEAD);
  252.  
  253. # Encrypt it if needed
  254. if ($encrypt) {
  255.     $ENV{'PGPPATH'} = "/www/.pgp";
  256.     open(TMPMAILBODY,"| pgp +batchmode +force -fea $path_info > /usr/tmp/mailform$$.body.out") || die "can't start pgp: $!";
  257.     push(@pgpout,"pgp +batchmode +force -fea $path_info > /usr/tmp/mailform$$.body.out\n");
  258. } else {
  259.     open(TMPMAILBODY,">/usr/tmp/mailform$$.body.out") || 
  260.     die "can\'t open /usr/tmp/mailform$$.body.out: $!";
  261. }
  262.  
  263. # Loop through fields, print fieldname: contents
  264. if ($sort) {
  265.     foreach $key (sort keys %in) {
  266.     ($pkey = $key) =~ s/^(\d+)//;
  267.         next    if ($pkey =~ /^($email|$subject|$comment)$/ && !$nospecial);
  268.         next    if ($pkey =~ /^(required|encrypt|sort|response|footers|nofieldname|nospecial|separator|footer)$/);
  269.     $in{$key} =~ s/\\0/; /g;
  270.     $in{$key} =~ s/^\s+; //;
  271.         print TMPMAILBODY "$pkey: "    unless ($in{'nofieldname'});
  272.     eval "print TMPMAILBODY \"\$in{\$key}$sep\"";
  273.     }
  274. } else {
  275.     foreach $key (keys %in) {
  276.     ($pkey = $key) =~ s/^(\d+)//;
  277.         next    if ($pkey =~ /^($email|$subject|$comment)$/ && !$nospecial);
  278.         next    if ($pkey =~ /^(required|encrypt|sort|response|footers|nofieldname|nospecial|separator|footer)$/);
  279.     ($pkey = $key) =~ s/^(\d+)//;
  280.     $in{$key} =~ s/\\0/; /g;
  281.     $in{$key} =~ s/^\s+; //;
  282.         print TMPMAILBODY "$pkey: "    unless ($in{'nofieldname'});
  283.     eval "print TMPMAILBODY \"\$in{\$key}$sep\"";
  284. #    print TMPMAILBODY "$in{$key}$sep";
  285.     }
  286. }
  287.  
  288. # Place comments as the body of the e-mail
  289. unless ($nospecial) {
  290.     print TMPMAILBODY "\n$in{$comment}\n"    if ($in{$comment});
  291.     print TMPMAILBODY "\n";
  292.     print TMPMAILBODY "-- \n";
  293. }
  294.  
  295. # Also generate a line to track what host sent it, just in case we
  296. # get a bogus e-mail address.
  297. print TMPMAILBODY "Received from: $ENV{'REMOTE_USER'}@$ENV{'REMOTE_HOST'}($ENV{'REMOTE_ADDR'})\n";
  298. close(TMPMAILBODY);
  299. close(STDERR);
  300.  
  301. # Get pgp output
  302. open(LOG,"</usr/tmp/mailform$$.log");
  303. while(<LOG>) {
  304.     push(@pgpout,$_);
  305. }
  306. close(LOG);
  307. &error(@pgpout) if grep(/Invalid|usage:/,@pgpout);
  308.  
  309. # Actually send the e-mail
  310. if ($in{$email}) {
  311.     open(MAIL,"|/usr/lib/sendmail -t -f $in{$email}");
  312. } else {
  313.     open(MAIL,"|/usr/lib/sendmail -t");
  314. }
  315. open(TMPMAIL,"/usr/tmp/mailform$$.head.out") || 
  316.     die "can\'t open /usr/tmp/mailform$$.head.out for input: $!";
  317. while(<TMPMAIL>) {
  318.     print MAIL $_;
  319. }
  320. close(TMPMAIL);
  321. open(TMPMAIL,"/usr/tmp/mailform$$.body.out") || 
  322.     die "can\'t open /usr/tmp/mailform$$.body.out for input: $!";
  323. while(<TMPMAIL>) {
  324.     print MAIL $_;
  325. }
  326. close(TMPMAIL);
  327. close(MAIL);
  328.  
  329. # Cleanup after ourselves
  330. unlink("/usr/tmp/mailform$$.head.out");
  331. unlink("/usr/tmp/mailform$$.body.out");
  332. unlink("/usr/tmp/mailform$$.log");
  333.  
  334. if ($response && 
  335.     (($response =~ /^\// && open(FILE,"$DocumentRoot$response") ||
  336.       open(FILE,"$response")))) {
  337.     print "Content-type: text/html\n\n";
  338.     while (<FILE>) {
  339.     print $_;
  340.     }
  341. } else {
  342.     &header("Thank you!");
  343.     print "<H2>Thank you for your comments!</H2>\n"; 
  344.     print "<H3>Your comments have been sent to $path_info</H3>";
  345.     &footer;
  346. }
  347.  
  348. if ($in{'ftype'} eq "ffb"){
  349.     @temp = split (/;/,$in{$email});
  350.     foreach (@temp){
  351.         if (/\@/){
  352.             $in{$email} = $_;
  353.             last;
  354.         }
  355.     }
  356.     open (MAIL,"|/usr/lib/sendmail -t");
  357.     print MAIL "To: $in{$email}\n";
  358.         print MAIL "From: webmaster\@www.sgi.com (Electronic Marketing at Silicon Graphics, Inc.)\n";
  359.     print MAIL "Subject: Your Message to Silicon Surf\n";
  360.     open (TEMP ,"<./autoreply.txt");
  361.     while (<TEMP>){
  362.         print MAIL "$_";
  363.     }
  364.     close (TEMP);
  365.     close (MAIL);
  366. }
  367.